const { app, BrowserWindow, globalShortcut } = require("electron");
const { Menu } = require("electron");
const serve = require("electron-serve");
const path = require("path");

const loadURL = serve({ directory: "./exported" });

const cp = require("child_process");

// 防止安装阶段多次启动
if (require("electron-squirrel-startup")) app.quit();

let win;

if (BrowserWindow.getAllWindows().length === 0) {
  // var bootBackEnd = cp.spawn(`${__dirname}/public/jre/bin/java`, [
  var bootBackEnd = cp.spawn(`java`, [
    "-jar",
    `${__dirname}/public/revise-web-1.0-SNAPSHOT.jar`,
  ]);
  bootBackEnd.stdout.on("data", (data) => {
    console.log(data.toString());
  });

  try {
    (async () => {
      await app.whenReady();
      win = new BrowserWindow();
      // win.webContents.openDevTools()
      win.setIcon(path.join(__dirname, "public/icons/png/256x256.png"));
      Menu.setApplicationMenu(null);
      await loadURL(win);
    })();
  } catch (e) {
    console.log(e);
  }
}

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
  app.quit();
});

app.on("quit", () => {
  bootBackEnd.kill();
});
